package stark.app.function.news;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import stark.app.R;
import stark.common.apis.ApiManager;
import stark.common.apis.base.NewsBean;
import stark.common.apis.base.NewsListBean;
import stark.common.apis.constant.NewsType;
import stark.common.base.IReqRetCallback;
import stark.common.basic.adaptermutil.StkLoadDataCallback;
import stark.common.basic.adaptermutil.StkLoadMoreModel;
import stark.common.basic.adaptermutil.StkProviderLoadMoreAdapter;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2022/12/1
 * Email：1027154338@qq.com
 * Desc:
 */
public class NewsAdapter extends StkProviderLoadMoreAdapter<NewsBean> {

    private NewsType newsType;
    private LifecycleOwner lifecycleOwner;

    public NewsAdapter(LifecycleOwner lifecycleOwner, NewsType newsType) {
        super(1);
        this.lifecycleOwner = lifecycleOwner;
        this.newsType = newsType;
        addItemProvider(new ItemProvider());
    }

    @Override
    protected void onConfig(@NonNull @NotNull StkLoadMoreModel.LoadConfig config) {
        //这里页面大小请设置为不超过30，因为后台要求最大为30，设为最大同时可减少请求次数，减少请求次数消耗量
        config.pageSize(30);
    }

    @Override
    public void reqLoadData(int page, int pageSize, @NonNull StkLoadDataCallback<NewsBean> callback) {
        ApiManager.newsApi().getNewsList(lifecycleOwner, newsType, page, new IReqRetCallback<NewsListBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable NewsListBean result) {
                if (result != null) {
                    callback.onLoadedData(success, result.data, result.hasMoreData());
                } else {
                    callback.onLoadedData(success, null, false);
                }
            }
        });
    }

    private class ItemProvider extends BaseItemProvider<NewsBean> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_news;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, NewsBean bean) {
            TextView tvTitle = holder.getView(R.id.tvTitle);
            tvTitle.setText(bean.title);

            RecyclerView rvPics = holder.getView(R.id.rvPics);
            rvPics.setLayoutManager(new GridLayoutManager(getContext(), 3));
            NewsImgAdapter imgAdapter = new NewsImgAdapter();
            imgAdapter.setNewInstance(bean.getPics());
            rvPics.setAdapter(imgAdapter);

            TextView tvCoverRv = holder.getView(R.id.tvCoverRv);
            tvCoverRv.setOnClickListener(v -> {
                OnItemClickListener listener = getOnItemClickListener();
                if (listener == null) return;
                listener.onItemClick(NewsAdapter.this, holder.itemView, holder.getAdapterPosition());
            });

            TextView tvAuthor = holder.getView(R.id.tvAuthor);
            tvAuthor.setText(bean.author_name);
        }
    }

}
